﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#include "GnufAppearanceSetting.h"
#include "GnufFamilyTypes.h"
#include "GnufNameValuePairs.h"
#include "GnufQtToolbar.h"
#include "GnufToolbar.h"
#include "GnufTree.h"
#include "QtGnufToolbar.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gnuf;
using namespace gfam;

QtGnufToolbar::QtGnufToolbar(const gcmp::UniqueString * uniqueKey, gnuf::IItemEventHandler * handler, QtGnufToolbar::EType type)
{
    m_toolbar = TransferOwnership(NEW_AS_OWNER_PTR(gnuf::Toolbar, uniqueKey));
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_toolbar, L"m_toolbar无效", L"paramodel", L"2024-02-20");
    m_toolbar->setEventHandler(handler);
    gnuf::Direction dir = gnuf::Direction::Horizontal;
    switch (type)
    {
    case QtGnufToolbar::eTree: {
        m_toolbar->setAppearanceSetting(NEW_AS_OWNER_PTR(
            gnuf::AppearanceSetting, 
            gnuf::AppearanceSetting::EStyle::eTree));
        m_toolbar->addItem<TreeBoard>();
    } break;

    case QtGnufToolbar::eFamilyType: {
        m_toolbar->setAppearanceSetting(NEW_AS_OWNER_PTR(
            gnuf::AppearanceSetting,
            gnuf::AppearanceSetting::EStyle::eFamilyTypes));
        m_toolbar->addItem<FamilyTypes>();
    } break;

    case QtGnufToolbar::eNameValuePairs: {
        m_toolbar->setAppearanceSetting(NEW_AS_OWNER_PTR(
            gnuf::AppearanceSetting,
            gnuf::AppearanceSetting::EStyle::eNameValuePairs));
        m_toolbar->addItem<NameValuePairs>();
        dir = gnuf::Direction::Vertical;
    } break;

    default:
        DBG_WARN(L"Don't support this type!", L"paramodel", L"2024-02-20");
        break;
    };

    m_qtToolbar = TransferOwnership(NEW_AS_OWNER_PTR(gnuf::QtToolbar, m_toolbar.get(), dir));
}

void QtGnufToolbar::destroy()
{
    if (nullptr == m_toolbar || nullptr == m_qtToolbar)
    {
        return;
    }

    // Disconnect the Item tree from the QtItem tree.
    m_toolbar->disconnectFromQtItem();

    // Delete immediately the Item tree.
    m_toolbar.reset();

    // Remove from the Qt instance tree.
    m_qtToolbar->setParent(nullptr);

    // Leave to Qt to delete later.
    gnuf::QtToolbar* pQtToolbar = m_qtToolbar.ReleaseOwnership();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pQtToolbar, L"pQtToolbar无效", L"paramodel", L"2024-02-20");
    pQtToolbar->deleteLater();
}

gnuf::TreeBoard * QtGnufToolbar::tree() const
{
    if (nullptr != m_toolbar &&
        1 == m_toolbar->contents().size() &&
        m_toolbar->contents().front() &&
        gnuf::EItemType::eTreeBoard == m_toolbar->contents().front()->type())
    {
        return const_cast<gnuf::TreeBoard *>(m_toolbar->contents().front()->cast<gnuf::TreeBoard>());
    }
    return nullptr;
}

gnuf::FamilyTypes * QtGnufToolbar::familyTypes() const
{
    if (nullptr != m_toolbar &&
        1 == m_toolbar->contents().size() &&
        m_toolbar->contents().front() &&
        gnuf::EItemType::eFamilyTypes == m_toolbar->contents().front()->type())
    {
        return const_cast<gnuf::FamilyTypes *>(m_toolbar->contents().front()->cast<gnuf::FamilyTypes>());
    }
    return nullptr;
}

gnuf::NameValuePairs * QtGnufToolbar::nameValuePairs() const
{
    if (nullptr != m_toolbar &&
        1 == m_toolbar->contents().size() &&
        m_toolbar->contents().front() &&
        gnuf::EItemType::eNameValuePairs == m_toolbar->contents().front()->type())
    {
        return const_cast<gnuf::NameValuePairs *>(m_toolbar->contents().front()->cast<gnuf::NameValuePairs>());
    }
    return nullptr;
}